<?

class BandInformation extends db {

	var $ID, $name, $bio, $miscellaneous, $address1, $address2;
	var $city, $stateProvince, $country, $postalCode, $managerName;
	var $defaultCity, $defaultStateProvince, $defaultCountry, $genreID;
	var $description;

	function getGenres() {
		$q = "select ID, genreName from Genres order by genreName asc";
		$r = mysql_query($q);
		if ($r) {
			$genres = array();
			while ($row = mysql_fetch_assoc($r)) {
				$genres[] = $row;
			}
			return $genres;
		} else {
			return Error::MySQL();
		}
	}
	
	function getGenreID() {
		return $this->genreID;
	}
	
	function getGenreName() {
		if ($this->genreID) {
			$q = "select genreName from Genres where ID = {$this->genreID}";
			$r = mysql_query($q);
			if ($r) {
				$row = mysql_fetch_assoc($r);
				return $row['genreName'];
			} else {
				return Error::MySQL();
			}
		}
	}
	
	function get() {
		$q = "select * from Band_Information";
		$db = new db;
		$r = mysql_query($q);
		if ($r) {
			$result = mysql_num_rows($r);
			if ($result > 0) { 
				$row = mysql_fetch_assoc($r);
				$bi = new BandInformation;
				foreach($row as $key => $value) {
					$bi->{$key} = $db->sanitize_from_db($value);
				}
				return $bi;
			} else {
				$bi = new BandInformation;
				return $bi;
			}
		} else {
			return Error::MySQL();
		}
	}
	
	function getID() {
		return $this->ID;
	}
	
	function getName() {
		return $this->name;
	}
	
	function getBio() {
		return $this->bio;
	}
	
	function getMiscellaneous() {
		return $this->miscellaneous;
	}
	
	function getManager() {
		return $this->managerName;
	}
	
	function getAddress1() {
		return $this->address1;
	}
	
	function getAddress2() {
		return $this->address2;
	}
	
	function getCity() {
		return $this->city;
	}
	
	function getStateProvince() {
		return $this->stateProvince;
	}
	
	function getPostalCode() {
		return $this->postalCode;
	}
	
	function getCountry() {
		return $this->country;
	}
	
	function getDefaultStateProvince() {
		return $this->defaultStateProvince;
	}
	
	function getDefaultCity() {
		return $this->defaultCity;
	}
	
	function getDefaultCountry() {
		return $this->defaultCountry;
	}
	
	function getCountryName() {
		return $this->_getCountryName($this->country);
	}
	
	function getDefaultCountryName() {
		return $this->_getCountryName($this->defaultCountry);
	}
	
	function getStateProvinceName() {
		return $this->_getStateProvinceName($this->stateProvince);
	}
	
	function getDefaultStateProvinceName() {
		return $this->_getStateProvinceName($this->defaultstateProvince);
	}

	function getDescription() {
		return $this->description;
	}

	function _getCountryName($country) {
		$cl = new CountryList();
		if ($cl->countryList[$country] != null) {
			return $cl->countryList[$country];
		}
	}
	
	function _getStateProvinceName($sp) {
		$sl = new StateList();
		if ($sl->stateProvinceList[$sp] != null) {
			return $sl->stateProvinceList[$sp];
		} else {
			return $sp;
		}
	}
	
	function isValidGenreID($genreID) {
		if (is_numeric($genreID) && $genreID > 0) {
			$q = "select ID from Genres where ID = {$genreID}";
			$r = mysql_query($q);
			if ($r) {
				$row = mysql_fetch_assoc($r);
				if ($row['ID']) {
					return true;
				}
			}
		}
	}
	
	function update($postArray) {
		$db = new db;	
		$e = new Error();
		if (User::isAdmin()) {			
			$genreID = $db->sanitize_to_db($postArray['genreID']);
			if (!$this->isValidGenreID($postArray['genreID'])) {
				$e->add("Invalid genre specified");	
			}
			
			$name = $db->sanitize_to_db($postArray['name']);
			if ($name == '' || $name == null) {
				$e->add("You must specify a name for your band.");
			}
			
			$managerName = $db->sanitize_to_db($postArray['managerName']);
			$address1 = $db->sanitize_to_db($postArray['address1']);
			$address2 = $db->sanitize_to_db($postArray['address2']);
			$city = $db->sanitize_to_db($postArray['city']);
			$stateProvince = $db->sanitize_to_db($postArray['stateProvince']);
			if ($stateProvince == "??") {
				$stateProvince = $db->sanitize_to_db($postArray['stateProvinceOther']);
			}
			$postalCode = $db->sanitize_to_db($postArray['postalCode']);
			$bio = $db->sanitize_to_db($postArray['bio']);
			$miscellaneous = $db->sanitize_to_db($postArray['miscellaneous']);
			$country = $db->sanitize_to_db($postArray['country']);
			
			$defaultStateProvince = $db->sanitize_to_db($postArray['defaultStateProvince']);
			if ($defaultStateProvince == "??") {
				$defaultStateProvince = $db->sanitize_to_db($postArray['defaultStateProvince']);
			}
			$defaultCountry = $db->sanitize_to_db($postArray['defaultCountry']);
			$defaultCity = $db->sanitize_to_db($postArray['defaultCity']);
			$description = $db->sanitize_to_db($postArray['description']);
			
			if ($e->hasErrors()) {
				return $e;
			}
			
			$q = "delete from Band_Information";
			$r = mysql_query($q);
			if (!$r) {
				return Error::MySQL();
			}			
			
			$q = "insert into Band_Information (name, managerName, address1, address2, city, stateProvince, postalCode, bio, miscellaneous, country, defaultStateProvince, defaultCountry, defaultCity, genreID, description) ";
			$q .= "values ('$name', '$managerName', '$address1', '$address2', '$city', '$stateProvince', '$postalCode', '$bio', '$miscellaneous', '$country', '$defaultStateProvince', '$defaultCountry', '$defaultCity', '$genreID', '$description')";
			$r = mysql_query($q);
			
			// ping auditionrocks.com
			// aborted attempt at creating an audition directory
			
			/*
			include_class('xmlrpc');
			$xc = new xmlrpc_client("/ping/", "www.auditionrocks.com");
			$message = new xmlrpcmsg("audition.pingBack", array(
					new xmlrpcval($_SERVER["HTTP_HOST"] . SITE_WEB_DIRECTORY, "string"),
					new xmlrpcval($name, "string"),
					new xmlrpcval($bio, "string"),
					new xmlrpcval($genreID, "int"),
					new xmlrpcval($city, "string"),
					new xmlrpcval($stateProvince, "string"),
					new xmlrpcval($postalCode, "string"),
					new xmlrpcval($country, "string"))
				);

			$response = $xc->send($message, 5, "POST");
			*/
			
			if ($r) {
				return true;
			} else {
				return Error::create("An unexplained error occurred when trying to update your information.");
			}
		}
	}
	
	
	
}

?>
